/*
 * Adapter.cpp
 *
 *  Created on: Sep 19, 2012
 *      Author: pedro
 */

#include "Adapter.h"

Graph * Adapter::applyConstraints(Graph &original, Problem &problem)
{
	Graph copy (original);

	for(int i = 0; i < copy.getSize(); ++i)
	{
		for(int j = 0; j < copy.getSize(); ++j)
		{
			if(problem.getConstraints()(i,j) == REMOVED)
			{
				copy(i,j,DASH);
			}
		}
	}

	Graph *reduced = new Graph (copy.getSize() - problem.getRemovedRowsSize());


	if(problem.getRemovedRowsSize() != 0)
	{
		int w = 0;
		for(int i = 0; i < copy.getSize(); ++i)
		{
			if(problem.findRemovedRow(i) == false)
			{
				int k = 0;
				for(int j = 0; j < copy.getSize(); ++j)
				{
					if(problem.findRemovedColumn(j) == false)
					{
						(*reduced)(w, k++,copy(i,j));
					}
				}
				w++;
			}
		}
	}
	else
	{
		for(int i = 0; i < copy.getSize(); ++i)
		{
			for(int j = 0; j < copy.getSize(); ++j)
			{
					(*reduced)(i, j,copy(i,j));
			}
		}
	}

	return reduced;

}

vector<int> Adapter::expandSolution(vector<int> &shortSolution, Problem &problem)
{
	int size = problem.getConstraints().getSize();
	vector<int> expandedSolution;
	int shift, difShift;

	if(problem.getRemovedRowsSize() > 0)
	{
		int j = 0;
		for(int i = 0; i < size; ++i)
		{
			if(problem.findRemovedRow(i))
			{
				expandedSolution.push_back(problem.getCorrespondentFixedColumn(i));
			}
			else
			{
				shift = problem.getShiftColumns(shortSolution[j]);
				difShift = problem.getShiftColumns(shortSolution[j] + shift);

				while(difShift > shift)
				{
					shift = problem.getShiftColumns(shortSolution[j]+difShift);
					difShift = problem.getShiftColumns(shortSolution[j]+shift);
				}

				expandedSolution.push_back(shortSolution[j++] + shift);
			}
		}
	}
	else
		return shortSolution;

	return expandedSolution;
}
